package com.crazypos.controller.admin;

import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.AttributeEntity;
import com.crazypos.pojo.AttributeValuesEntity;
import com.crazypos.pojo.bo.AdminCreateAttributeBO;
import com.crazypos.pojo.bo.AdminCreateAttributeValueBO;
import com.crazypos.pojo.bo.AdminDeleteAttributeBO;
import com.crazypos.security.entity.UserDetailEntity;
import com.crazypos.service.IAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/attribute")
public class AdminAttributeController {

    @Autowired
    private IAttributeService attributeService;


    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getAttributeList() {
        String organizationId = SecurityUtil.getOrganizationId();
        List<AttributeEntity> result = attributeService.getAttributeList(organizationId);
        return ApiRestResponse.success(result);
    }

    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createAttribute(@RequestBody AdminCreateAttributeBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        attributeService.createAttribute(bo, userInfo.getId(), userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @PostMapping("/delete")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse deleteAttribute(@RequestBody AdminDeleteAttributeBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        attributeService.deleteAttribute(bo, userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @GetMapping("/list_value")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse getAttributeValueList(@RequestParam Integer attributeId) {
        List<AttributeValuesEntity> result = attributeService.getValuesByAttributeId(attributeId);
        return ApiRestResponse.success(result);
    }

    @PostMapping("/create_value")
    @PreAuthorize("hasRole('ADMIN')")
    public ApiRestResponse createAttributeValue(AdminCreateAttributeValueBO bo) {
        attributeService.createAttributeValue(bo);
        return ApiRestResponse.success();
    }
}
